<?php
namespace app\common\model;
use service\LogApiService;
use think\Db;
use think\Validate;

class Company extends Common
{
    private $company_table = 'hn_company';
    private $table_name = 'hn_company';
    private $table_name1 = 'hn_ompanyContact';

    /** 封装一个企业的基本信息，*/
    public function fillCompany($name, $logo, $info)
    {
        $id = 1;
        $model = ['id' => $id, 'name' => $name, 'logo' => $logo, 'info' => $info];
        return $model;
    }
    /** 获取企业信息 */
    public function getCompanyInfo()
    {
        $model = Db::query("SELECT * FROM $this->table_name WHERE id=:id", ['id' => 1]);
        if (sizeof($model) > 0) {
            return $model[0];
        }
        else {
            return $this->fillCompany('', '', '');
        }
    }
    /** 修改企业信息 */
    public function updateCompanyIofo($id, $new_info)
    {
        $model = $this->getCompanyInfo();
        if (is_null($model)) {
            return ['flag' => \FALSE, 'msg' => '没有此企业'];
        }

        if ($model['name'] != $new_info['name'] && strlen($new_info['name']) > 1) {
            $model['name'] = $new_info['name'];
        }
        if ($model['logo'] != $new_info['logo'] && strlen($new_info['logo']) > 1) {
            $model['logo'] = $new_info['logo'];
        }
        if ($model['info'] != $new_info['info'] && strlen($new_info['info']) > 1) {
            $model['info'] = $new_info['info'];
        }

        $rule = [
            'name' => 'require|max:50',
            'logo' => 'require|max:128',
            'info' => 'require'
        ];
        $message = [
            'name.require' => '企业名称不能为空',
            'name.max' => '企业名称长度为 1-50 个字符',
            'logo.require' => '请上传企业商标图',
            'info.require' => '请输入企业介绍'
        ];

        $validate = new Validate($rule, $message);

        if (!$validate->check($model)) {
            return ['flag' => \FALSE, 'msg' => $validate->getError()];
        }

        $res = Db::execute(
            "UPDATE $this->table_name SET name=:name, logo=:logo, info=:info WHERE id=:id",
            ['id' => $id, 'name' => $model['name'], 'logo' => $model['logo'], 'info' => $model['info']]
        );

        if ($res > 0){
            return ['flag' => \TRUE, 'data' => $model];
        }else{
            return ['flag' => \TRUE, 'msg' => '数据库操作失败！'];
        }
    }

    /** 以下是企业联系方式 */

    /** 封装一个企业联系方式 */
    /**
     * @param $target
     * @param $phone
     * @param $mobile
     * @param $fax
     * @param $addr
     * @param $email
     * @param $qq
     * @param $wechat
     * @return array
     */
    public function fillContact($target, $phone, $mobile, $fax, $addr, $email, $qq, $wechat)
    {
        $id = 1;
        $model = [
            'id' => $id, 'target' => $target, 'phone' => $phone, 'mobile' => $mobile,
            'fax' => $fax, 'email' => $email, 'qq' => $qq, 'wechat' => $wechat
        ];
        return $model;
    }
    /** 获取企业的联系方式 */
    public function getCompanyContact()
    {
        $model = Db::query("SELECT * FROM $this->table_name1 WHERE id=:id", ['id' => 1]);
        if (sizeof($model) > 0) {
            return $model[0];
        }
        else {
            return $this->fillContact('铭诚科技', '020-88888888', '13888888888', '020-88888889', '广州市天河区', 'kf@mcsys.cn', '1234', 'msn@mcsys.cn');
        }
    }


    /**
     * 修改企业的联系方式
     * @param $id
     * @param $new_contact
     * @return array
     */
    public function updateCompanyCantact($id, $new_contact)
    {
        $model = $this->getCompanyContact();
        if (is_null($model)) {
            return ['flag' => \FALSE, 'msg' => '没有此企业联系方式'];
        }

        $new_info=[];

        if ($model['target'] != $new_info['target'] && strlen($new_info['target']) > 1) {
            $model['target'] = $new_info['target'];
        }
        if ($model['phone'] != $new_info['phone'] && strlen($new_info['phone']) > 1) {
            $model['phone'] = $new_info['phone'];
        }
        if ($model['mobile'] != $new_info['mobile'] && strlen($new_info['mobile']) > 1) {
            $model['mobile'] = $new_info['mobile'];
        }
        if ($model['fax'] != $new_info['fax'] && strlen($new_info['fax']) > 1) {
            $model['fax'] = $new_info['fax'];
        }
        if ($model['email'] != $new_info['email'] && strlen($new_info['email']) > 1) {
            $model['email'] = $new_info['email'];
        }
        if ($model['qq'] != $new_info['qq'] && strlen($new_info['qq']) > 1) {
            $model['qq'] = $new_info['qq'];
        }
        if ($model['wechat'] != $new_info['wechat'] && strlen($new_info['wechat']) > 1) {
            $model['wechat'] = $new_info['wechat'];
        }

        $rule = [
            'target' => 'max:50',
            'phone' => 'max:40',
            'mobile' => 'max:40',
            'fax' => 'max:40',
            'email' => 'max:100',
            'qq' => 'max:20',
            'wechat' => 'max:128'
        ];
        $message = [
            'target.max' => '企业名称不能为空',
            'phone.max' => '企业名称长度为 1-50 个字符',
            'mobile.max' => '请上传企业商标图',
            'fax.max' => '请输入企业介绍',
            'email.max' => '',
            'qq.max' => '',
            'wechat' => ''
        ];

        $validate = new Validate($rule, $message);

        if (!$validate->check($model)) {
            return ['flag' => \FALSE, 'msg' => $validate->getError()];
        }

        $res = Db::execute(
            "UPDATE $this->table_name1 SET target=:target, phone=:phone, mobile=:mobile, fax=:fax, email=:email, qq=:qq, wechat=:wechat WHERE id=:id",
            [
                'id' => $id, 'target' => $model['target'], 'phone' => $model['phone'], 'mobile' => $model['mobile'],
                'fax' => $model['fax'], 'email' => $model['email'], 'qq' => $model['qq'], 'wechat' => $model['wechat']
            ]
        );
        
        if ($res > 0){
            return ['flag' => \TRUE, 'data' => $model];
        }else{
            return ['flag' => \TRUE, 'msg' => '数据库操作失败！'];
        }
    }


    /**
     * 保存公司信息
     * @param $data
     * @return int|string
     */
    public function saveCompany($data)
    {

        $need=["id","name","logo","info","target","phone","mobile","fax","addr","email","qq","wechat"];

        $r=$this->getNeed($need,$data);

        $res=$this->saveData($r,$this->company_table);

        if($res){
            return $res !== false;
        }else{
            return $res !== false;
        }
    }
}